# -*- coding: utf-8 -*-
"""
    Created by huangyi at 2020/7/29.
    Description:
    Changelog: all notable changes to this file will be documented
"""
from ...expression.context import Context
from ...expression.exception.func_not_found_exception import FuncNotFoundException
from ...expression.nonterminal_expression import NonterminalExpression


class FuncExpression(NonterminalExpression):
    """
    方法表达式
    """

    def interpret(self, ctx: Context):
        func = ctx.get_func(self.func_name)
        if not func:
            raise FuncNotFoundException("找不到函数:" + self.func_name)
        expression_results = []
        # 所有参数的表达式要先进行计算
        for param_exp in self.params:
            expression_results.append(param_exp.interpret(ctx))
        return func.calc(ctx, *expression_results)

    def __init__(self, func_name, *param_expression):
        self.params = list(param_expression)
        self.func_name = func_name
